clear
set more off
set matsize 5000

global lecind    "\\intra\partages\au_amic2\SRCV\TABLES_INITIALES\INPUT\INDIVIDUS"
global lecindsas "\\intra\partages\au_amic2\SRCV_AVRIL2021\TABLES_INITIALES_AVRIL2021\INDIVIDUS"
global ecrind    "\\intra\partages\au_amic2\SRCV_AVRIL2021\BASES_FEV2022_V5\INDIVIDUS\autres_var"
global ecrstat   "\\intra\partages\au_amic2\SRCV_AVRIL2021\STAT_fev2022_V5\2004_2008"

/* 1 - EXTRACTION DES VARIABLES SELECTIONNéES */
/*     fichier STATA 1ère livraison 2004-2008 */
foreach an of numlist 8 {
cd "$lecind"
use individus0`an'_diff.dta, clear
keep ind men age adultvrairep idmenc_* idindc_* /// 
sexe nbhpr statut cs24 nbtsal ressex_a ressex_b ressex_c ressex_d ressex_e ressex_f ressex_g ressex_h dip14 naf2 occupa situa actif actoccup pl080 
gen annee_SRCV = 200`an'
order men ind annee_SRCV adultvrairep 
rename idmenc_* idmenc
rename idindc_* idindc
rename naf2 NAF2_2003
cd "$ecrind"
save autres_indiv_`an'_bis.dta, replace
display `an'
count 
}

foreach an of numlist 4/7 {
cd "$lecind"
use individus0`an'_diff.dta, clear
keep ind men age adultvrairep idmenc_* idindc_* /// 
sexe nbhpr statut cs24 nbtsal ressex_a ressex_b ressex_c ressex_d ressex_e ressex_f ressex_g ressex_h dip14 naf2 occupa situa actif actoccup pl080 nbmois_cho
gen annee_SRCV = 200`an'
order men ind annee_SRCV adultvrairep 
rename idmenc_* idmenc
rename idindc_* idindc
rename naf2 NAF2_2003
cd "$ecrind"
save autres_indiv_`an'.dta, replace
display `an'
count 
}

/* 2 - EMPILEMENT DES TABLES */
clear 
use "$ecrind\autres_indiv_8_bis.dta", clear
append using 	"$ecrind\autres_indiv_4.dta" "$ecrind\autres_indiv_5.dta" ///
                "$ecrind\autres_indiv_6.dta" "$ecrind\autres_indiv_7.dta" 
sort  men ind annee_SRCV idmenc  
order men ind annee_SRCV idmenc 
save "$ecrind\autres_individus_2004_2008.dta", replace


/* 3 - MISE EN FORME DES VARIABLES */
* 3-1 DESTRING
clear
use "$ecrind\autres_individus_2004_2008.dta", clear
*situa
tab situa annee_SRCV, missing
tab situa , missing
destring situa, replace
tab situa , missing
*actif
tab actif annee_SRCV, missing
tab actif , missing
destring actif, replace
tab actif , missing
*actoccup
tab actoccup annee_SRCV, missing
tab actoccup , missing
destring actoccup, replace
tab actoccup , missing
*statut
tab statut annee_SRCV, missing
tab statut , missing
destring statut, replace
tab statut , missing
*nbtsal
tab nbtsal annee_SRCV, missing
tab nbtsal , missing
destring nbtsal, replace
tab nbtsal , missing
*sexe
tab sexe annee_SRCV, missing
tab sexe , missing
destring sexe, replace
tab sexe , missing
*DIP14
tab dip14 annee_SRCV, missing
tab dip14 , missing
destring dip14, replace
tab dip14 , missing
*CS24
tab cs24 annee_SRCV, missing
tab cs24 , missing
replace cs24="" if cs24 == "**"
tab cs24 , missing
destring cs24, replace
tab cs24 , missing
*NAF2_2003
tab NAF2_2003 annee_SRCV, missing
tab NAF2_2003 , missing
replace NAF2_2003="" if NAF2_2003 == "**"
tab NAF2_2003 , missing
destring NAF2_2003, replace
tab NAF2_2003 , missing
save "$ecrind\autres_individus_2004_2008_def.dta", replace

* 3-2 CREATION DE DIP14_new (fusion de 2008 à 2010 des 60 et 70 en 67)
clear
use "$ecrind\autres_individus_2004_2008_def.dta", clear
tab dip14, missing
gen dip14_new = dip14
replace dip14_new=67 if dip14 == 60 
replace dip14_new=67 if dip14 == 70 
tab dip14 annee_SRCV, missing
tab dip14_new annee_SRCV, missing
save "$ecrind\autres_individus_2004_2008_def.dta", replace

* 3-3 CREATION VARIABLE NUMERIQUE SITUA_NEW HOMOGENEISEE SUR TOUTE LA PERIODE AVEC 7 modalités : inutile ici
*     on garde seulement les 2 variables situa et situa_new en vue du merge avec période 2008-2019
clear
use "$ecrind\autres_individus_2004_2008_def.dta", clear
tab situa, missing
gen situa_new = situa
*replace situa_new=7 if situa == 8 
*tab situa annee_SRCV, missing
*tab situa_new annee_SRCV, missing
save "$ecrind\autres_individus_2004_2008_def.dta", replace

/* 4 - CREATION DE YEAR */
clear
use "$ecrind\autres_individus_2004_2008_def.dta", clear
gen year = annee_SRCV - 1
sort  men ind annee_SRCV idmenc  
order men ind annee_SRCV year idmenc idindc age adultvrairep NAF2_2003 dip14 dip14_new situa situa_new
save "$ecrind\autres_individus_2004_2008_def.dta", replace


/* 5 - CREATION DE LA VARIABLE DIPLOME */
clear
use "$ecrind\autres_individus_2004_2008_def.dta", clear
gen DIPLOME = .
replace DIPLOME = 1 if dip14_new == 10
replace DIPLOME = 1 if dip14_new == 12
replace DIPLOME = 1 if dip14_new == 20 
replace DIPLOME = 2 if dip14_new == 30 
replace DIPLOME = 3 if dip14_new == 31
replace DIPLOME = 4 if dip14_new == 33 
replace DIPLOME = 5 if dip14_new == 41 
replace DIPLOME = 6 if dip14_new == 42
replace DIPLOME = 6 if dip14_new == 43
replace DIPLOME = 6 if dip14_new == 44
replace DIPLOME = 7 if dip14_new == 50
replace DIPLOME = 8 if dip14_new == 67
replace DIPLOME = 9 if dip14_new == 71
tab dip14_new DIPLOME, missing  
tab dip14_new, missing  
tab DIPLOME, missing  
 
sort  men ind annee_SRCV idmenc  
order men ind annee_SRCV year idmenc idindc age adultvrairep dip14 dip14_new DIPLOME NAF2_2003 situa situa_new
save "$ecrind\autres_individus_2004_2008_def.dta", replace
